home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 15 / macformat_15.iso / Shareware Internet / Desarrolladores / gray image 2.1 / vimage_io.cc < prev    next >
C/C++ Source or Header  |  1995-06-05  |  3KB  |  114 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *
  5.  *               Grayscale Image
  6.  *
  7.  *          Verify reading/writing of various image formats
  8.  *
  9.  * $Id: vimage_io.cc,v 1.1 1994/02/07 15:50:58 oleg Exp $
  10.  *
  11.  ************************************************************************
  12.  */
  13.  
  14. #include "image.h"
  15. #include "std.h"
  16. #include <iostream.h>
  17.  
  18. static IMAGE Test_image(16,32,8); //(256,512,8);
  19.  
  20. #ifdef __GNUC__
  21. #include <builtin.h>
  22. #endif
  23.                     // Verify the pixels have the value
  24.                     // that is expected
  25. static void verify_pixel_value(const GRAY val)
  26. {
  27.   register int i,j;
  28.   for(i=0; i<Test_image.q_nrows(); i++)
  29.     for(j=0; j<Test_image.q_ncols(); j++)
  30.       if( Test_image(i,j) != val )
  31.     _error("Pixel [%d,%d] has the value 0x%x different from expected 0x%x",
  32.            i,j,Test_image(i,j),val);
  33. }
  34.  
  35. static char * Image_file_name = "/tmp/aa";
  36.  
  37.                 // Write different kinds of image file formats
  38.                 // into the file named Image_file_name
  39. static void write_xwd(const IMAGE& image)
  40. {
  41.   image.write_xwd(Image_file_name);
  42. }
  43.  
  44. static void write_pgm(const IMAGE& image)
  45. {
  46.   image.write_pgm(Image_file_name);
  47. }
  48.  
  49. static void write_tiff(const IMAGE& image)
  50. {
  51.   image.write_tiff(Image_file_name);
  52. }
  53.  
  54. static void write_default(const IMAGE& image)
  55. {
  56.   image.write(Image_file_name);
  57. }
  58.  
  59.  
  60.                 // Check reading and writing of a test image
  61. static void test_image_io(void (*writer)(const IMAGE& image))
  62. {
  63.   cout << "\n\n\tWriting the Test_image and reading it back\n";
  64.   Test_image = 154; Test_image(0,0) = 0; Test_image(1,0) = 1;
  65.   writer(Test_image);
  66.   IMAGE read_back(Image_file_name,1);
  67.   Test_image -= read_back;
  68.   verify_pixel_value(0);
  69.  
  70.   cout << "\nDone\n";
  71. }
  72.  
  73.                 // Test the I/O using a real image
  74. static void real_image_io(const IMAGE& image, 
  75.               void (*writer)(const IMAGE& image))
  76. {
  77.   cout << "\n\n\tWriting a real image and reading it back\n";
  78.   
  79.   start_timer();
  80.   writer(image);
  81.   cout << "\nIt took " << return_elapsed_time(0) 
  82.        << " sec to write the image\n";
  83.  
  84.   start_timer();
  85.   IMAGE read_again_image(Image_file_name);
  86.   cout << "\nIt took " << return_elapsed_time(0) 
  87.       << " sec to read the image\n";
  88.   assert( read_again_image == image );
  89.  
  90.   cout << "\nDone\n";
  91. }
  92.  
  93.  
  94.  
  95. main(void)
  96. {
  97.   cout << "\n\nTest image input/output operations\n";
  98.  
  99.   test_image_io(write_xwd);
  100.   test_image_io(write_pgm);
  101.   test_image_io(write_tiff);
  102.   test_image_io(write_default);
  103.  
  104. #ifdef __MWERKS__
  105.   IMAGE image(":pictures:512.pgm");
  106. #else
  107.   IMAGE image("pictures/512.xwd");
  108. #endif
  109.   real_image_io(image,write_xwd);
  110.   real_image_io(image,write_pgm);
  111.   real_image_io(image,write_tiff);
  112.   real_image_io(image,write_default);
  113. }
  114.